#include    "weather.h"
#include	"setting.h"
LV_IMG_DECLARE(yingtian);
LV_IMG_DECLARE(sandian);
LV_IMG_DECLARE(qing);
LV_IMG_DECLARE(feng);
LV_IMG_DECLARE(duoyun);
LV_IMG_DECLARE(dayu);
LV_IMG_DECLARE(daxue);
LV_IMG_DECLARE(yu);
LV_IMG_DECLARE(yingtian);
LV_IMG_DECLARE(xue);
LV_IMG_DECLARE(wu);
LV_FONT_DECLARE(siyuanrouti_20);

LV_IMG_DECLARE(paozhu_1);
LV_IMG_DECLARE(paozhu_2);
LV_IMG_DECLARE(beizhu_1);
LV_IMG_DECLARE(beizhu_2);
LV_IMG_DECLARE(beizhu_3);
LV_IMG_DECLARE(beizhu_4); 

static const lv_img_dsc_t* anim_imgs_1[2] = {
    &paozhu_1,
    &paozhu_2,
};

static const lv_img_dsc_t* anim_imgs_2[4] = {
    &beizhu_1,
    &beizhu_2,
    &beizhu_3,
	&beizhu_4,
};
//时间
extern int year;
extern unsigned char month;
extern unsigned char day;
extern unsigned char sec_cnt;
extern unsigned char min_cnt;
extern unsigned char Real_hour;
extern unsigned char xingqiji;
//天气温度城市
extern char City[];
extern char temp;
extern unsigned char weather_code;
extern lv_indev_t * indev_keypad;
extern lv_group_t * g;
lv_obj_t *label_city=NULL;
lv_obj_t *label_date=NULL;
lv_obj_t *label_temp=NULL;
lv_obj_t *label_weather=NULL;
lv_obj_t *label_time=NULL;
lv_obj_t *label_weekday=NULL;
lv_timer_t * weather_Clock_Update=NULL;
lv_timer_t * weather_temp_Update=NULL;

lv_obj_t * icon; 
extern uint8_t yuyan_flag;   //0为英文

//设置今天的礼拜日
void Set_xingqi(void){
	switch(xingqiji){
		case 0: if(yuyan_flag==1)lv_label_set_text(label_weekday,"Sunday");if(yuyan_flag==0)lv_label_set_text(label_weekday,"礼拜天");break;
		case 1: if(yuyan_flag==1)lv_label_set_text(label_weekday,"Monday");if(yuyan_flag==0)lv_label_set_text(label_weekday,"礼拜一");break;
		case 2: if(yuyan_flag==1)lv_label_set_text(label_weekday,"Tuesday");if(yuyan_flag==0)lv_label_set_text(label_weekday,"礼拜二");break;
		case 3: if(yuyan_flag==1)lv_label_set_text(label_weekday,"Wednesday");if(yuyan_flag==0)lv_label_set_text(label_weekday,"礼拜三");break;
		case 4: if(yuyan_flag==1)lv_label_set_text(label_weekday,"Thursday");if(yuyan_flag==0)lv_label_set_text(label_weekday,"礼拜四");break;
		case 5: if(yuyan_flag==1)lv_label_set_text(label_weekday,"Friday");if(yuyan_flag==0)lv_label_set_text(label_weekday,"礼拜五");break;
		case 6: if(yuyan_flag==1)lv_label_set_text(label_weekday,"Saturday");if(yuyan_flag==0)lv_label_set_text(label_weekday,"礼拜六");break;
		defualt:lv_label_set_text(label_weekday,"Unknown");if(yuyan_flag==1)lv_label_set_text(label_weekday,"未知");break;
	}

}
//根据时间设置背景颜色
void Set_backgroud_color(lv_obj_t*obj){
	//白天
	if(Real_hour>=6&&Real_hour<=18){
	lv_obj_set_style_bg_color(obj,lv_color_hex(0x39BAF4),NULL);
	}
	//黑夜
	else{
	lv_obj_set_style_bg_color(obj,lv_color_hex(0x7B7B7B),NULL);
	}


}


//clock update task
void Clock_Update_task(lv_timer_t * timer){
	//跟新时间
	lv_label_set_text_fmt(label_time,"%02d:%02d:%02d",Real_hour,min_cnt,sec_cnt);
	//更新礼拜几
	Set_xingqi();
	//更新日期
	lv_label_set_text_fmt(label_date,"%4d/%02d/%02d",year,month,day);
}
//weather_code
//根据天气code设置相应的天气以及图案动画
void set_the_weather(){
	switch(weather_code){
	case 0:
	case 1:
	case 2:
	case 3:lv_label_set_text(label_weather,"晴");lv_img_set_src(icon, &qing);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 4:
	case 5:
	case 6:
	case 7:
	case 8:lv_label_set_text(label_weather,"多云");lv_img_set_src(icon, &duoyun);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 9:lv_label_set_text(label_weather,"阴");lv_img_set_src(icon, &yingtian);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 10:lv_label_set_text(label_weather,"阵雨");lv_img_set_src(icon, &sandian);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 11:
	case 12:lv_label_set_text(label_weather,"雷阵雨");lv_img_set_src(icon, &sandian);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 13:lv_label_set_text(label_weather,"小雨");lv_img_set_src(icon, &yu);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 14:lv_label_set_text(label_weather,"中雨");lv_img_set_src(icon, &yu);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 15:lv_label_set_text(label_weather,"大雨");lv_img_set_src(icon, &dayu);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 16:lv_label_set_text(label_weather,"暴雨");lv_img_set_src(icon, &dayu);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 17:lv_label_set_text(label_weather,"特大暴雨");lv_img_set_src(icon, &dayu);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 20:lv_label_set_text(label_weather,"雨夹雪");lv_img_set_src(icon, &xue);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 22:lv_label_set_text(label_weather,"小雪");lv_img_set_src(icon, &xue);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 23:lv_label_set_text(label_weather,"中雪");lv_img_set_src(icon, &xue);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 24:lv_label_set_text(label_weather,"大雪");lv_img_set_src(icon, &daxue);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 25:lv_label_set_text(label_weather,"暴雪");lv_img_set_src(icon, &daxue);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 30:lv_label_set_text(label_weather,"霾");lv_img_set_src(icon, &wu);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	case 32:
	case 33:lv_label_set_text(label_weather,"风");lv_img_set_src(icon, &feng);lv_obj_align(icon,LV_ALIGN_CENTER,40,10);break;
	default:lv_label_set_text(label_weather,"Unknow");break;
	}
}

//回到funcion界面
void weather_to_funcion(lv_event_t*e){
	lv_obj_t*obj=lv_event_get_target(e);
	lv_group_remove_obj(obj);
	lv_indev_set_group(indev_keypad, g);
	lv_timer_del(weather_Clock_Update);
	lv_timer_del(weather_temp_Update);
	back_to_funcion(obj);
}


//temp update task  2min跟新一次
void temp_Update_task(lv_timer_t * timer){
	lv_obj_t*obj=(lv_obj_t*)timer->user_data;
	getCityandWeather();
	//更新气温
	lv_label_set_text_fmt(label_temp,"%2d",temp);
	//更新天气
	set_the_weather();
	//跟新背景
	Set_backgroud_color(obj);
}

void Weather_Init(lv_obj_t*obj){
	static lv_style_t style_label;
	static lv_obj_t *label_C=NULL;
	lv_style_init(&style_label); 
	lv_style_set_text_color(&style_label,lv_color_hex(0x000000));
	//create the label of the city
	label_city=lv_label_create(obj);
	lv_obj_add_style(label_city,&style_label,NULL);
	lv_obj_set_style_text_font(label_city,&siyuanrouti_20,0);
	if(strcmp(City,"成都")==0){
		if(yuyan_flag==1)	lv_label_set_text(label_city,"ChengDu");
		else if(yuyan_flag==0) lv_label_set_text(label_city,"成都");
	}
	else if(strcmp(City,"重庆")==0){
		if(yuyan_flag==1)	lv_label_set_text(label_city,"ChongQing");
		else if(yuyan_flag==0) lv_label_set_text(label_city,"重庆");
	}
	else if(strcmp(City,"绵阳")==0){
		if(yuyan_flag==1)	lv_label_set_text(label_city,"MianYang");
		else if(yuyan_flag==0) lv_label_set_text(label_city,"绵阳");
	}
	else if(strcmp(City,"达州")==0){
		if(yuyan_flag==1)	lv_label_set_text(label_city,"DaZhou");
		else if(yuyan_flag==0) lv_label_set_text(label_city,"达州");
	}
	lv_obj_align(label_city,LV_ALIGN_TOP_MID,0,10);

	//设置背景颜色
	Set_backgroud_color(obj);
	//设置礼拜几
	label_date=lv_label_create(obj);
	lv_obj_add_style(label_date,&style_label,NULL);
	lv_obj_set_style_text_font(label_date,&siyuanrouti_20,0);
	lv_label_set_text_fmt(label_date,"%4d/%02d/%02d",year,month,day);
	lv_obj_align(label_date,LV_ALIGN_TOP_LEFT,20,40);
	//设置温度
	label_temp=lv_label_create(obj);
	lv_obj_add_style(label_temp,&style_label,NULL);
	lv_obj_set_style_text_font(label_temp,&lv_font_montserrat_48,0);
	lv_obj_align(label_temp,LV_ALIGN_LEFT_MID,0,15);
	lv_label_set_text_fmt(label_temp,"%2d",temp);
	//设置温度记号c
	label_C=lv_label_create(obj);																																																																																																																																																																																																															
	lv_obj_add_style(label_C,&style_label,NULL);																																						
	lv_obj_align(label_C,LV_ALIGN_LEFT_MID,50,0);
	lv_label_set_text(label_C,"C");
	//设置星期几
    label_weekday=lv_label_create(obj);
	lv_obj_add_style(label_weekday,&style_label,NULL);
	lv_obj_set_style_text_font(label_weekday,&siyuanrouti_20,0);
	Set_xingqi();
	lv_obj_align_to(label_weekday,label_temp,LV_ALIGN_OUT_TOP_MID,40,-5);
	//设置天气
	label_weather=lv_label_create(obj);
	lv_obj_add_style(label_weather,&style_label,NULL);
	lv_obj_set_style_text_font(label_weather,&siyuanrouti_20,0);
	icon = lv_img_create(obj);
	set_the_weather();
	lv_obj_align(label_weather,LV_ALIGN_LEFT_MID,55,20);
	weather_temp_Update=lv_timer_create(temp_Update_task,120000, NULL);
	//设置时间
	label_time=lv_label_create(obj);																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																											
	lv_obj_add_style(label_time,&style_label,NULL);
	lv_label_set_text_fmt(label_time,"%02d:%02d:%02d",Real_hour,min_cnt,sec_cnt);
	lv_obj_set_style_text_font(label_time,&siyuanrouti_20,0);
	lv_obj_align(label_time,LV_ALIGN_TOP_LEFT,20,140);
    weather_Clock_Update=lv_timer_create(Clock_Update_task,200, NULL);

	//创建两个装饰动画
   lv_obj_t * animimg0 = lv_animimg_create(obj);
   lv_obj_align(animimg0,LV_ALIGN_CENTER,-30,95);
   lv_animimg_set_src(animimg0, (lv_img_dsc_t**) anim_imgs_1, 2);
   lv_animimg_set_duration(animimg0, 300);
   lv_animimg_set_repeat_count(animimg0,LV_ANIM_REPEAT_INFINITE);
   lv_animimg_start(animimg0);

   lv_obj_t * animimg1 = lv_animimg_create(obj);
   lv_obj_align(animimg1,LV_ALIGN_CENTER,30,90);
   lv_animimg_set_src(animimg1, (lv_img_dsc_t**) anim_imgs_2, 4);
   lv_animimg_set_duration(animimg1, 300);
   lv_animimg_set_repeat_count(animimg1,LV_ANIM_REPEAT_INFINITE);
   lv_animimg_start(animimg1);

	lv_group_add_obj(g,obj);
	lv_indev_set_group(indev_keypad, g);
	//create one btn back to the function page
	lv_obj_add_event_cb(obj,weather_to_funcion,LV_EVENT_CLICKED,NULL);
}
